package models

import (
	"t11Lib/t11Errors"
	"t11Lib/t11Model"
)

//保修卡
type Guarantee struct {
	t11Model.BaseMdl //包含创建时间
	UserId           string
	Info             string //保修卡信息，手机号、地址等。可以是前端傳過來的json字符串
}
type guranteeDao struct {
}

var GuranteeDao = guranteeDao{}

func (self guranteeDao) CreateOrUpdate(conn *t11Model.DbConn, userId, devId, Info string) t11Errors.T11Error {
	obj := &Guarantee{}
	obj.UserId = userId
	obj.Id = devId
	obj.Info = Info
	_, err := CreateOrUpdate(conn, obj, "Info")
	return err
}

func (self guranteeDao) ListByUser(conn *t11Model.DbConn, userId string) ([]*Guarantee, t11Errors.T11Error) {
	var ret []*Guarantee
	_, err := conn.QueryTable(&Guarantee{}).Filter("UserId", userId).All(&ret)
	return ret, t11Errors.Wrap(err)
}

func (self guranteeDao) ListByDevIds(conn *t11Model.DbConn, devIds []string) ([]*Guarantee, t11Errors.T11Error) {

	var ret []*Guarantee
	var err t11Errors.T11Error

	for _, devId := range devIds {
		var guarantee = &Guarantee{}
		guarantee.Id = devId
		if err = t11Errors.Wrap(conn.Read(guarantee)); err != nil {
			return nil, err
		} else {
			ret = append(ret, guarantee)
		}
	}
	return ret, t11Errors.Wrap(err)
}

func (self guranteeDao) GetByDevId(conn *t11Model.DbConn, devId string) (*Guarantee, t11Errors.T11Error) {
	var ret = &Guarantee{}
	ret.Id = devId
	err := conn.Read(ret)
	return ret, t11Errors.Wrap(err)
}
